// userStore.js
import { defineStore } from 'pinia'
import { loginAPIa, loginAPIb } from '@/apis/user'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  // 管理两个用户的状态
  const userAInfo = ref({
    userId:null,
    username:'',
    message:'',
  })
  const userBInfo = ref({})

  // 获取用户A的信息
  const getUserAInfo = async ({ account, password }) => {
    const res = await loginAPIa({ account, password })
    // userAInfo.value = res;
    // console.log('Updated userAInfo:', userAInfo.value); // 验证数据是否成功更新
    const { data } = res; // 提取返回值中的 data 对象
    userAInfo.value = {
        userId: data.userId,
        username: data.username,
        message: data.message,
    };
    console.log('Updated userAInfo:', userAInfo.value);
  }

  // 获取用户B的信息
  const getUserBInfo = async ({ account, password }) => {
    const res = await loginAPIb({ account, password })
    userBInfo.value = res
  }
//退出时清除用户信息
const clearUserInfo = () => {
  userAInfo.value = {}
  userBInfo.value = {}
}

  return {
    userAInfo,
    userBInfo,
    getUserAInfo,
    getUserBInfo,
    clearUserInfo
  } 
},
{
  persist: true,
}
)
